#!/bin/bash
. /opt/hygon/common.sh

grub_file=/etc/default/grub

add_cmdline()
{
    local result1
    local result2
    cmd="$1"
    csv3_cmdline=`cat ${grub_file} | grep ^GRUB_CMDLINE | grep "csv_mem_percentage=[0-9][0-9]"`
    result1=$?
    csv3_cmdline=`cat ${grub_file} | grep ^GRUB_CMDLINE | grep "csv_mem_percentage=[0-9][ |\"]"`
    result2=$?
    if [ $result1 -eq 0 ];then
        sed -i 's/csv_mem_percentage=[0-9][0-9]/'${cmd}'/g' ${grub_file}
    elif [ $result2 -eq 0 ];then
        sed -i 's/csv_mem_percentage=[0-9]/'${cmd}'/g' ${grub_file}
    else
        sed -i 's!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 '${cmd}'"!' ${grub_file}
    fi
}

os_release=$(eval get_os_release)

default=50

case $os_release in
     "anolis")
             add_cmdline "csv_mem_percentage=${default}"
             grub2-mkconfig -o /boot/efi/EFI/anolis/grub.cfg
             ;;
     "kylin")
             add_cmdline "csv_mem_percentage=${default}"
             grub2-mkconfig -o /boot/efi/EFI/kylin/grub.cfg
             ;;
     "centos")
             add_cmdline "csv_mem_percentage=${default}"
             grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
             ;;
     "ubuntu")
             add_cmdline "csv_mem_percentage=${default}"
             update-grub
             ;;
esac
